/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package statistics;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Moustafa Samir
 */
public class CurveSmoothing {

    public static ArrayList<Double> smooth(ArrayList<Double> list, int k) {
        Object[]objectArray = list.toArray();
        Double[] data =  Arrays.copyOf(objectArray, objectArray.length, Double[].class);
        Double[] smoothed = new Double[data.length];
        //calculate the elements at the start and the end of the array at the same time
        double startSum = data[0];
        double endSum = data[data.length-1];
        smoothed[0] = data[0];
        smoothed[data.length-1] = data[data.length-1];
        int noOfElements = 1;
        for (int i = 1; i <= k; i++) {
            noOfElements += 2;
            int index1 = 2*i;
            int index2 = 2*i-1;

            startSum += data[index1]+data[index2];
            smoothed[i] = startSum/(noOfElements);

            endSum += data[data.length-index1-1]+data[data.length-index2-1];
            smoothed[data.length-i-1] = endSum/(noOfElements);
        }
        //calculate the elements in the middle
        int end = data.length-k-1;
        for (int i = k+1; i <= end; i++) {

            startSum = startSum -data[i-k-1]+data[i+k];
            smoothed[i] = startSum/(noOfElements);
        }
        ArrayList<Double>smoothedArray = new ArrayList<Double>(Arrays.asList(smoothed));
        return smoothedArray;
    }

    public void loadFile (String filePath,int k)
    {
        ArrayList<Double> hourlyThrouphut = new ArrayList<Double>();
        try
        {
         FileInputStream fstream = new FileInputStream(filePath);

          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br.readLine()) != null)
          {
              hourlyThrouphut.add(Double.parseDouble(strLine));
             // System.out.println (strLine);
          }
          in.close();
        }catch (Exception e)
        {
         System.err.println("Error: " + e.getMessage());
        }
       
        try
        {
            SystemAnalysis.saveData("smoothingResult", smooth(hourlyThrouphut, k));
        } catch (IOException ex) {
            Logger.getLogger(CurveSmoothing.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) {
//        ArrayList<Double> al = new ArrayList<Double>();
//        al.add(3.0);
//        al.add(5.0);
//        al.add(10.0);
//        al.add(1.0);
//        al.add(3.0);
//        al.add(3.0);
//        al.add(9.0);
//        smooth(al, 2);
        CurveSmoothing cs = new CurveSmoothing();
        cs.loadFile("HourlyThroughput.csv", 20);
    }
}